<?php
/*
 *      [Discuz! X] (C)2012-2012 bbs.heirui.cn.
 *      This is NOT a freeware, use is subject to license terms
 *
 *      $Id: pay.inc.php 1303 2012-12-08 17:29:26Z yanyuxiaoyao $
 */
if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}
$version='2.1.2';
$cvar= $_G['cache']['plugin']['yy_pay'];
$pid=intval($_GET['pid']);
$cid=intval($_GET['cid']);
$credit=0;
if($_G['uid']){
	$r=DB::fetch_first("SELECT * FROM ".DB::table("common_member_count")." WHERE uid=$_G[uid]");
	$rs=DB::fetch_first("SELECT * FROM ".DB::table("yy_pay")." WHERE p_pid=$pid and p_cid=$cid");
	$rss=DB::fetch_first("SELECT * FROM ".DB::table("yy_pay_log")." WHERE pl_pid=$pid and pl_cid=$cid and pl_uid=$_G[uid]");
	if($r)$credit=$r['extcredits'.$cvar['p_credit']];
	if(!$rs)showmessage(lang('plugin/yy_pay','var31'));
	if($rss)showmessage(lang('plugin/yy_pay','var30'));
	if($_GET['submit']!='yes')
		include template('yy_pay:pay');
	else{
		if($credit>=$rs['p_pay']){
			$author=getuserbyuid($rs['p_authorid']);
			load_yypayprosetting();
			$pay_pro=$_G['load_yyplugin']['yy_pay']['pro'];
			$gjsd['bl']=$cvar['p_bl'];
			if($pay_pro[$author['groupid']])
				$gjsd=$pay_pro[$author['groupid']];
			$income=ceil((($rs['p_pay']*$gjsd['bl'])/100));
			// $income=(int)(($rs['p_pay']*$gjsd['bl'])/100);
			
			//扣调查看的那个人的钱
			updatemembercount($_G['uid'], array('extcredits'.$cvar['p_credit'] => "-".$rs['p_pay']), true, '', 0, '');//减调钱
			//增加到作者的钱
			updatemembercount($rs['p_authorid'], array('extcredits'.$cvar['p_credit'] => $income), true, '', 0, '');

			DB::query("INSERT INTO ".DB::table("yy_pay_log")."(pl_pid,pl_cid,pl_uid,pl_uname,pl_authorid,pl_authorincome,pl_pay,pl_extcredit) VALUES($pid,$cid,$_G[uid],'$_G[username]',$rs[p_authorid],$income,$rs[p_pay],$cvar[p_credit])");
			showmessage(lang('plugin/yy_pay','var12'),$_GET['referer']);
		}else{
			showmessage(lang('plugin/yy_pay','var13'),$_GET['referer']);
		}
	}
}else{
	showmessage('to_login', NULL, array(), array('login' => 1));
}
function load_yypayprosetting(){
		global $_G;
		if(isset($_G['load_yyplugin']['yy_pay']['pro']))
			return;
		if($_G['setting']['version']=='X2.5')
			$dircache='sysdata';
		else
			$dircache='cache';
		if(file_exists(DISCUZ_ROOT.'./data/'.$dircache.'/cache_yy_pay_pro.php'))
			require_once DISCUZ_ROOT.'./data/'.$dircache.'/cache_yy_pay_pro.php';
		$_G['load_yyplugin']['yy_pay']['pro']=$pay_pro;
	}
?>